package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.MarketCouponService;
import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MarketCouponServiceImpl implements MarketCouponService {
    @Autowired
    private MarketCouponMapper couponMapper;
    @Override
    public List<MarketCoupon> doList(Integer page, Integer limit, String name, Integer status, String sort, String order) {
        Integer currentPage = page;
        Integer pagesize = limit;
        
        
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        if (name == null ) {
            //不输入内容，查询整个表单
            // select * from marketAd
            marketCouponExample.orderBy(sort + " " + order);
            marketCouponExample.or().andIdIsNotNull().andDeletedNotEqualTo(true);
            PageHelper.startPage(currentPage, pagesize);
            List<MarketCoupon> marketCoupons = couponMapper.selectByExample(marketCouponExample);
            
            
            return marketCoupons;
        }


        if (name != null ) {
            marketCouponExample.or().andNameLike("%" + name + "%").andDeletedNotEqualTo(true);
            PageHelper.startPage(currentPage, pagesize);
            marketCouponExample.orderBy(sort + " " + order);
            List<MarketCoupon> marketCoupons = couponMapper.selectByExample(marketCouponExample);
            
            
            return marketCoupons;
        }


        marketCouponExample.or().andNameLike("%" + name + "%").andDeletedNotEqualTo(true);;
        PageHelper.startPage(currentPage, pagesize);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(marketCouponExample);
        
        
        return marketCoupons;
    }

//    @Override
//    public List<MarketCoupon> list() {
//        
//        
//        MarketCouponExample marketCouponExample = new MarketCouponExample();
//        marketCouponExample.or();
//        PageHelper.startPage(1, 20);
//        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(marketCouponExample);
//        
//        
//        return marketCoupons;
//    }


    @Override
    public int delete(Integer id) {
        
        
        int i=couponMapper.logicalDeleteByPrimaryKey(id);
        
        
        return i;
    }

    @Override
    public MarketCoupon update(String name, String desc, String tag, BigDecimal min, Short limit, Integer total, Short days, Integer id, Short type) {
        
        
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCoupon marketCoupon = new MarketCoupon();
        marketCoupon.setName(name);
        marketCoupon.setDesc(desc);
        marketCoupon.setTag(tag);
        marketCoupon.setMin(min);
        marketCoupon.setLimit(limit);
        marketCoupon.setTotal(total);
        marketCoupon.setDays(days);
        marketCoupon.setId(id);
        marketCoupon.setType(type);
        couponMapper.updateByPrimaryKey(marketCoupon);
        
        
        return marketCoupon;
    }

    @Override
    public MarketCoupon read(Integer id) {
        
        
        MarketCoupon marketCoupon = couponMapper.selectByPrimaryKey(id);
        
        
        return marketCoupon;

    }

    @Override
    public void listUser(HttpServletResponse resp) {
        try {
            resp.getWriter().print("{\n" +
                    "    \"errno\": 0,\n" +
                    "    \"data\": {\n" +
                    "        \"total\": 0,\n" +
                    "        \"pages\": 0,\n" +
                    "        \"limit\": 20,\n" +
                    "        \"page\": 0,\n" +
                    "        \"list\": [\n" +
                    "            \n" +
                    "        ]\n" +
                    "    },\n" +
                    "    \"errmsg\": \"成功\"\n" +
                    "}");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public MarketCoupon create(MarketCoupon marketCoupon) {
        
        

        marketCoupon.setAddTime(LocalDateTime.now());
        marketCoupon.setUpdateTime(LocalDateTime.now());
        couponMapper.insert(marketCoupon);
        MarketCoupon respMarketCoupon = couponMapper.selectByPrimaryKey(marketCoupon.getId());
        
        
        return respMarketCoupon;

    }
}
